package com.onlygx.dva.web.base.controller;

import com.onlygx.dva.common.entity.common.RT;
import com.onlygx.dva.common.entity.dto.CoreUserDTO;
import com.onlygx.dva.config.annotation.user.CoreUser;
import com.onlygx.dva.redis.service.RedisService;
import com.onlygx.dva.web.base.service.CommonAliSmsService;
import com.onlygx.dva.web.base.service.PublicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 手机号验证码发送实验
 *
 */
@Api(tags = "API - 工具")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/tools")
public class ApiToolsController {

    final PublicService publicService;
    final CommonAliSmsService smsService;

    final RedisService redisService;

    @PostMapping("/putObj")
    public RT<String> putName(String key, String value){
        redisService.set(key,value,5, TimeUnit.MINUTES);
        return RT.success();
    }

    @PostMapping("/findObj")
    public RT<String> findName(String key){
        String value = (String)redisService.get(key);
        return RT.success("获取成功",value);
    }

    /**
     * 发送手机验证码
     * 手机 2022年1月5日19:31:46 创建 高祥
     */
    @ApiOperation(value = "发送手机验证码",tags = "工具相关",notes = "发送手机验证码")
    @PostMapping("/sendCodeSMS")
    public RT<Void> sendCodeSMS(@CoreUser CoreUserDTO userDTO, @ApiParam("手机号") @RequestParam String phone) throws Exception {
        String code = smsService.sendRandom6Code(phone);
        publicService.savePhoneCode(userDTO.getUid(),code);
        return RT.success();
    }


}
